package com.ly.airline.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.airline.pojo.Hangban;
import com.ly.airline.pojo.Order;
import com.ly.airline.pojo.Orderitem;
import com.ly.airline.pojo.User;
import com.ly.airline.service.HangbanService;
import com.ly.airline.service.OrderService;
import com.ly.airline.service.OrderitemService;
import com.ly.airline.utils.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ：xxl
 * @ClassName OrderController
 * @date ：Created in 2022/4/29 3:28
 * @description： 订单接口
 * @modified By：
 * @version: 1.0
 */
@Controller
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderitemService orderitemService;

    @Autowired
    private HangbanService hangbanService;

    /**
     *@param current
	* @param pageSize
	* @param model
     * @return java.lang.String
     * @author xxl
     * @description: 后台查询订单列表
     * @date 2022/4/30 0:36
     */
    @GetMapping("getOrderList/{current}/{pageSize}")
    public String getOrderList(@PathVariable Integer current, @PathVariable Integer pageSize, Model model){
        IPage page = new Page(current,pageSize);
        orderService.page(page);
        model.addAttribute("pagers",page);
        return "admin/order/orderMana";
    }

    /**
     *@param current
	* @param pageSize
	* @param model
	* @param request
     * @return java.lang.String
     * @author xxl
     * @description: 前台查询用户订单列表
     * @date 2022/4/30 0:36
     */
    @GetMapping("getMyOrderList/{current}/{pageSize}")
    public String getMyOrderList(@PathVariable Integer current, @PathVariable Integer pageSize, Model model, HttpServletRequest request){
        //设置分页
        IPage page = new Page(current,pageSize);
        //获取登录数据
        User user = (User) request.getSession().getAttribute("user");
        //查询用户订单
        LambdaQueryWrapper<Order> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Order::getUserId,user.getUserId());
        orderService.page(page,queryWrapper);
        model.addAttribute("pagers",page);
        return "qiantai/order/orderMine";
    }

    /**
     *@param current
	* @param pageSize
	* @param id
	* @param model
     * @return java.lang.String
     * @author xxl
     * @description: 后台查询订单详情
     * @date 2022/4/30 0:36
     */
    @GetMapping("getOrderItemList/{current}/{pageSize}")
    public String getOrderItemList(@PathVariable Integer current, @PathVariable Integer pageSize,String id, Model model){
        //设置分页
        IPage page = new Page(current,pageSize);
        //查询指定订单的订单项
        LambdaQueryWrapper<Orderitem> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Orderitem::getOrderid,id);
        orderitemService.page(page,queryWrapper);
        List<Orderitem> orderitems = page.getRecords();
        //遍历添加航班数据
        orderitems.forEach(e->{
            Hangban hangban = hangbanService.getById(e.getHangbanid());
            e.setHangban(hangban);
        });
        page.setRecords(orderitems);
        model.addAttribute("pagers",page);
        model.addAttribute("orderId",id);
        return "admin/order/orderDetail";
    }

    /**
     *@param current
	* @param pageSize
	* @param id
	* @param model
     * @return java.lang.String
     * @author xxl
     * @description: 前台查询订单详情
     * @date 2022/4/30 0:36
     */
    @GetMapping("getMyOrderItem/{current}/{pageSize}")
    public String getMyOrderItem(@PathVariable Integer current, @PathVariable Integer pageSize,String id, Model model){
        IPage page = new Page(current,pageSize);
        LambdaQueryWrapper<Orderitem> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Orderitem::getOrderid,id);
        orderitemService.page(page,queryWrapper);
        List<Orderitem> orderitems = page.getRecords();
        orderitems.forEach(e->{
            Hangban hangban = hangbanService.getById(e.getHangbanid());
            e.setHangban(hangban);
        });
        page.setRecords(orderitems);
        model.addAttribute("pagers",page);
        model.addAttribute("orderId",id);
        return "qiantai/order/orderDetail";
    }

    /**
     *@param id
     * @return com.ly.airline.utils.HttpResult
     * @author xxl
     * @description: 后台删除订单
     * @date 2022/4/30 0:36
     */
    @GetMapping("deleteOrder/{id}")
    @ResponseBody
    public HttpResult deleteOrder(@PathVariable String id){
        orderService.removeById(id);
        return HttpResult.success(true,"删除成功");
    }

    /**
     *@param id
     * @return com.ly.airline.utils.HttpResult
     * @author xxl
     * @description: 后台受理订单
     * @date 2022/4/30 0:36
     */
    @GetMapping("receiveOrder/{id}")
    @ResponseBody
    public HttpResult receiveOrder(@PathVariable String id){
        Order order = orderService.getById(id);
        //改变订单状态
        order.setZhuangtai("已受理");
        //更新状态
        orderService.updateById(order);
        return HttpResult.success(true,"受理成功");
    }
}
